package com.lysj.print.print.controller;

import com.lysj.admin.utils.constant.CommonConstant;
import com.lysj.admin.utils.spring.foundation.base.BaseController;
import com.lysj.admin.utils.spring.foundation.pagination.PageUtil;
import com.lysj.admin.utils.spring.foundation.pagination.PageVo;
import com.lysj.admin.utils.validation.annotation.Valid;
import com.lysj.admin.utils.web.ParamUtil;
import com.lysj.admin.utils.web.Resp;
import com.lysj.print.print.domain.Printer;
import com.lysj.print.print.service.PrinterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.transaction.Transactional;

/**
 * @author Created by drj on 2019-03-15 16:40:54
 * @description 打印机控制层
 */
@Slf4j
@RestController
@RequestMapping("/print/printer")
public class PrinterController extends BaseController<Printer> {

    @Resource
    private PrinterService printerService;

    @Override
    public PrinterService getService() {
        return printerService;
    }

    @GetMapping("/list_rewrite")
    public Resp listRewrite(String storeId, PageVo pageVo) {
        Pageable pageable = PageUtil.initPage(pageVo);
        return new Resp().success(printerService.getRepository().findByStoreIdAndDelFlag(pageable, storeId, CommonConstant.NORMAL_FLAG));
    }

    @Override
    public Resp save(@Valid Printer entity) {
        return super.save(entity);
    }

    /*
     * @author drj
     * @date 2019-03-18 16:54
     * @Description :设为默认打印机
     */
    @Transactional
    @PostMapping("/set_default")
    public Resp setDefault(String storeId, String printerId) {
        Printer printer = printerService.findOne(printerId);
        if (ParamUtil.isBlank(printer)) {
            return new Resp().error(Resp.Status.PARAM_ERROR, "查无此打印机");
        }
        //如果当前打印机为默认打印机
        if (Printer.IsDefault.DEFAULT.getCode().equals(printer.getIsDefault())) {
            printer.setIsDefault(Printer.IsDefault.NORMAL.getCode());
            printerService.getRepository().save(printer);
            return new Resp().success("操作成功");
        }
        //查询本门店默认打印机,并设置为正常状态
        Printer defaultPrinter = printerService.getRepository().findByStoreIdAndIsDefaultAndDelFlag(storeId, Printer.IsDefault.DEFAULT.getCode(), CommonConstant.NORMAL_FLAG);
        if (!ParamUtil.isBlank(defaultPrinter)) {
            defaultPrinter.setIsDefault(Printer.IsDefault.NORMAL.getCode());
            printerService.getRepository().save(defaultPrinter);
        }
        //修改当前打印机为默认状态
        printer.setIsDefault(Printer.IsDefault.DEFAULT.getCode());
        printerService.getRepository().save(printer);
        return new Resp().success("操作成功");
    }

    @GetMapping("/select_item_rewrite")
    public Resp selectItemRewrite(String storeId) {
        return new Resp().success(printerService.getRepository().selectItems(storeId));
    }


}
